// 全局请求封装
const base_url = 'http://127.0.0.1:8080'
// 请求超出时间
const timeout = 3600000
 
// 需要修改token，和根据实际修改请求头
export default (params) => {
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let header = {
		'token': uni.getStorageSync('token') || '',
		...params.header
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method: method,
			header: header,
			data: data,
      timeout: timeout,
			success(response) {
				const res = response
        resolve(res);
			},
			fail(err) {
				console.log(err)
				if (err.errMsg.indexOf('request:fail') !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);
 
			},
			complete() {
				uni.hideLoading();
				uni.hideToast();
			}
		});
	}).catch(() => {});
};